package com.jf.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jf.blog.bean.Blog;
import com.jf.blog.bean.Comment;
import com.jf.blog.service.*;
import com.jf.blog.util.DateRangeUtils;
import com.jf.blog.util.LoginUtil;
import com.jf.blog.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@ResponseBody
@RequestMapping("/echarts")
public class EchartsController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private TagService tagService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private ApiService apiService;

    @GetMapping("/count")
    public ResultJson getCount(int roleChange, int timeChange) {
        Map<String, Long> map = new HashMap<>();
        Integer createUser = null;
        if (roleChange == 2) {
            createUser = LoginUtil.getLoginUser().getUserinfoId();
        }
        String[] timeRange;
        switch (timeChange) {
            case 2:
                timeRange = DateRangeUtils.getToday();
                break;
            case 3:
                timeRange = DateRangeUtils.getLastWeek();
                break;
            case 4:
                timeRange = DateRangeUtils.getLastMonth();
                break;
            case 5:
                timeRange = DateRangeUtils.getLastThreeMonths();
                break;
            case 6:
                timeRange = DateRangeUtils.getLastHalfYear();
                break;
            case 7:
                timeRange = DateRangeUtils.getLastYear();
                break;
            default:
                timeRange = new String[]{};
                break;
        }

        long blogCount = blogService.count(setQW(createUser, timeRange));
        long categoryCount = categoryService.count(setQW(createUser, timeRange));
        long tagCount = tagService.count(setQW(createUser, timeRange));
        long userinfoCount = userinfoService.count(setQW(createUser, timeRange));

        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        if (createUser != null) {
            /*获取当前用户的所有博客id*/
            List<Blog> blogList = blogService.list(new QueryWrapper<Blog>().eq("create_user", createUser));
            List<Integer> blogIds = blogList.stream().map(Blog::getBlogId).collect(Collectors.toList());
            commentQueryWrapper.in("blog_id", blogIds);
        }
        if (timeRange.length > 0) {
            commentQueryWrapper.between("create_time", timeRange[0], timeRange[1]);
        }

        long commentCount = commentService.count(commentQueryWrapper);

        map.put("blogCount", blogCount);
        map.put("categoryCount", categoryCount);
        map.put("tagCount", tagCount);
        map.put("commentCount", commentCount);
        map.put("userinfoCount", userinfoCount);
        return ResultJson.success(map);
    }

    private <T> QueryWrapper<T> setQW(Integer createUser, String[] timeRange) {
        QueryWrapper<T> qw = new QueryWrapper<>();
        qw.eq(createUser != null, "create_user", createUser);
        if (timeRange.length > 0) {
            qw.between("create_time", timeRange[0], timeRange[1]);
        }
        return qw;
    }

    @GetMapping("/bar/{createUser}")
    public ResultJson getBarOption(@PathVariable Integer createUser) {
        if (createUser == 1) {
            createUser = null;
        } else {
            createUser = LoginUtil.getLoginUser().getUserinfoId();
        }
        return apiService.getTimeline(createUser);
    }

    @GetMapping("/pie/{createUser}")
    public ResultJson getPieOption(@PathVariable Integer createUser) {
        if (createUser == 1) {
            createUser = null;
        } else {
            createUser = LoginUtil.getLoginUser().getUserinfoId();
        }
        return blogService.getBlogCategory(createUser);
    }
}
